<?php

/**
 * \file
 * Classe d'un bloc de listing d'un objet.
 */

namespace pilar\Scaffolding /* \cond */ ; /* \endcond */
{

/**
 * Bloc de listing. Il génère du xml.
 *
 * \todo Améliorer la gestion des actions.
 *
 * \since 23/09/2008
 * \author Dirk MARSKI <dirk.marski@gmail.com>
 */
class Listing extends Pagination {

protected function loadDefaultParams() {
 parent::loadDefaultParams();

 $this->params->define(array(
  'entity' => '',  // Entité dont on veut le listing.
  'editPage' => '',
  'paramDeleteIdName' => 'id',
  'deleteParams' => array(),
  'editParams' => array(),
  'defaultRedirection' => 'getBack'
 ));
}


protected function initialize() {
 $this->params->localId = '_'.$this->get('entity')->get('name');

 $this->addRedirection(new \pilar\Redirections\Redirection(array(
  'name' => 'default',
  'redirection' => $this->get('defaultRedirection')
 )));

 parent::initialize();
}


protected function load() {
 parent::load();

 $this->addContentPrecedingFile(
  $this->get('entity')->touchFile()
 );
}


protected function actions() {
 parent::actions();

 /*
  * TODO
  * 
  * Vérifier que l'ensemble des variables $_GET existe et sont valides.
  */
 
 if(isset($_GET['action']) && $_GET['action'] == 'delete') {
  // Suppression de l'objet avec l'identifiant envoyé :
   $this->get('entity')->byId($_GET[$this->get('paramDeleteIdName')])->delete();
   $this->redirection('default')->redirect();
 }
}


public function buildContentRoot($domDoc) {
 $domListing = $domDoc->createElement('listing');

 // Description des membres :
  $domMeta = $domDoc->createElement('meta');
  foreach($this->get('entity')->members as $member) {
   $domMember = $domDoc->createElement('member');
   $domMember->setAttribute('name', $member->get('name'));
   $domMember->setAttribute('caption', $member->get('caption'));

   $domMeta->appendChild($domMember);
  }
  $domListing->appendChild($domMeta);

 // Ajout de paramètres additionnels :
  $params = $this->get('deleteParams');
  $additionalDeleteParams='';
  foreach ($params as $paramName => $paramValue) {
   $additionalDeleteParams .= '&'.$paramName.'='.$paramValue;
  } 

  $params = $this->get('editParams');
  $additionalEditParams='';
  foreach ($params as $paramName => $paramValue) {
   $additionalEditParams .= '&'.$paramName.'='.$paramValue;
  } 

 // Données :
  foreach($this->_members as $item) {
   $domItem = $domDoc->createElement('item');

   $domItem->setAttribute('deleteLink', '?action=delete&'.$this->get('paramDeleteIdName').'='.$item->__get('id')->get().$additionalDeleteParams);
   
   if($this->get('editPage')) {
    $domItem->setAttribute('editLink', $this->get('editPage').'?id='.$item->__get('id')->get().$additionalEditParams);
   }

   $domItem->appendChild($item->domNode_meta($domDoc));

   $domListing->appendChild($domItem);
  }

 // Pagination :
  $domListing->appendChild(parent::buildContentRoot($domDoc));
 
 return $domListing;
}


protected function getMembers() {
 $objects = $this->get('entity')->newCollection();
 if ($this->get('paginationLength') > 0) {
  $objects->limit($this->get('paginationLength'), $this->_offset);
 }
 if ($this->get('entity')->get('titleMemberName') != '') {
//  $objects->orderBy($objects->member($this->get('entity')->get('titleMemberName')));
 }

 foreach ($objects as $object) {
  $this->_members[] = $object;
 }
}


} // Class.

} // Namespace.

?>
